using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion

	public class DiemHocTapRepository
	{
        public static List<DiemHocTap> XemDiem(string mssv, string namhoc, string hocky)
        {
            try
            {
                int iNamHoc, iHocKy;
                var lst = new List<DiemHocTap>();

                int.TryParse(namhoc, out iNamHoc);
                int.TryParse(hocky, out iHocKy);

                var query = from n in DataContext.Instance.DiemHocTap
                            where
                                n.ChiTietDangKy.PhieuDangKy.MSSV.Equals(string.Empty.Equals(mssv) ? n.ChiTietDangKy.PhieuDangKy.MSSV : mssv) &&
                                n.ChiTietDangKy.PhieuDangKy.KeHoachDaoTao.NamHoc.Value == (string.Empty.Equals(namhoc) ? n.ChiTietDangKy.PhieuDangKy.KeHoachDaoTao.NamHoc.Value : iNamHoc) &&
                                n.ChiTietDangKy.PhieuDangKy.KeHoachDaoTao.HocKy.Value == (string.Empty.Equals(hocky) ? n.ChiTietDangKy.PhieuDangKy.KeHoachDaoTao.HocKy.Value : iHocKy)
                            group n by n.ChiTietDangKy.PhieuDangKy.KeHoachDaoTao.NamHoc into g
                            select g.OrderBy(n2 => n2.ChiTietDangKy.PhieuDangKy.KeHoachDaoTao.HocKy);

                foreach (var group in query)
                {
                    foreach (var item in group)
                    {
                        lst.Add(item);
                    }
                }

                return lst;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return new List<DiemHocTap>();
        }

		public static List<DiemHocTap> SelectAll()
		{
			try
			{
				return DataContext.Instance.DiemHocTap.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<DiemHocTap>();
		}

		public static DiemHocTap SelectByID(int maht)
		{
			try
			{
				return DataContext.Instance.DiemHocTap.FirstOrDefault(item => item.MaHT == maht );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(DiemHocTap obj)
		{
			try
			{
				DataContext.Instance.DiemHocTap.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int maht)
		{
			try
			{
				var delitem = DataContext.Instance.DiemHocTap.FirstOrDefault(item => item.MaHT == maht );
				DataContext.Instance.DiemHocTap.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<DiemHocTap> RetrieveByID(int maht)
		{
			try
			{
				return (from item in DataContext.Instance.DiemHocTap where  item.MaHT == maht  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<DiemHocTap>();
		}

		public static List<DiemHocTap> SelectByMaCTDK(int mactdk)
		{
			try
			{
			    var lstItem = (from item in DataContext.Instance.DiemHocTap where item.MaCTDK == mactdk select item).ToList();
			    return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<DiemHocTap>();
		}

	}

}
